package com.baiyang.server.service.read.impl;


import com.alibaba.fastjson.JSONObject;
import com.baiyang.server.model.read.Bill;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.service.read.BillService;
import com.baiyang.server.tools.DataTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.read.BalanceDao;
import com.baiyang.server.model.read.Balance;
import com.baiyang.server.service.read.BalanceService;

/**
 * 
 * @author XVX
 * BY: @Primary 注解帮助生命@Autowired优先注入的实列
 */
@Service	
public class BalanceServiceImpl extends BaseServiceImpl<BalanceDao, Balance> implements BalanceService{

    @Autowired
    private BalanceDao balanceDao;
    @Autowired
    private BillService billService;

    /**
     * 更改余额记录账单
     * @param userId
     * @param money
     * @return
     */
    @Override
    public JSONObject recharge(String userId, String money) {
        Balance balance = new Balance();
        balance.setUserId(userId);
        balance = selectOne(balance);
        balance.setBalance(balance.getBalance() + Double.valueOf(money)*10);
        balanceDao.updateById(balance);
        Bill bill = new Bill();
        bill.setId(DataTool.createUUID());
        bill.setType("1");
        bill.setUserId(userId);
        bill.setAmount(Double.valueOf(money));
        bill.setNum(Double.valueOf(money)*10);
        billService.insert(bill);
        JSONObject object = new JSONObject();
        object.put("bookCoin",String.valueOf(balance.getBalance()));
        return object;
    }
}
